﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
/// <summary>
/// Summary description for ClassName
/// </summary>
public class AuditManager
{
    public AuditManager()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    
    public static void Log(string fieldChange,string type,string oldValue,string newValue,string user,string custid,string ccid){
        try{
            string sql = @"INSERT INTO AuditTrail
                         (FieldChange,TypeOfChange,OldValue,NewValue,ChangeBy,CustomerID,CashCardNo,DateCreated)
                         VALUES(@0,@1,@2,@3,@4,@5,@6,@7)";
            
            
             List<object> param = new List<object>();
             param.Add(fieldChange);
             param.Add(type);
             param.Add(oldValue);
             param.Add(newValue);
             param.Add(user);
             param.Add(custid);
             param.Add(ccid);
             param.Add(DateTime.Now);
            
             var query = DBManager.Execute(sql,param.ToArray());
                                                        
        }catch(Exception e){
            throw e;
        }
    }
    
    public static IList<dynamic> GetLogs(Dictionary<string,string> p){
        try{
            string sql = @"SELECT * FROM AuditTrail";
            StringBuilder sb = new StringBuilder();
            sb.Append(sql);
            
            if(p["FromDate"]!=null && p["ToDate"]!=null){
                //sb.Append(" WHERE DateCreated BETWEEN to_date('").Append(p["FromDate"]).Append("')");
                //sb.Append(" AND to_date ('").Append(p["ToDate"]).Append("')");
                
                sb.Append(" WHERE DateCreated >= to_date('").Append(p["FromDate"]).Append("')");
                sb.Append(" AND DateCreated <= to_date ('").Append(p["ToDate"]).Append("')");
            }
            var query = DBManager.Query(sb.ToString(),null);
            return query;
        }catch(Exception e){
            throw e;
        }
        
    }
}
